home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 311_01 / db_main.c < prev    next >
C/C++ Source or Header  |  1990-04-21  |  48KB  |  1,936 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      db_main.c  v1.3  (c) 1987-1990  Ken Harris                          */
  5. /*                                                                          */
  6. /*                                                                          */
  7. /****************************************************************************/
  8. /*                                                                          */
  9. /*      This software is made available on an AS-IS basis. Unrestricted     */
  10. /*      use is granted provided that the copyright notice remains intact.   */
  11. /*      The author makes no warranties expressed or implied.                */
  12. /*                                                                          */
  13. /****************************************************************************/
  14.  
  15. #include "dblib.h"
  16.  
  17. static char  *copyright = "db V1.3 (c) 1987-1990 Ken Harris";
  18.  
  19. int    db_error     = 0;
  20. ulong  db_match_blk = 0;
  21. ushort db_match_rec = 0;
  22. ulong  db_add_blk   = 0;
  23. ushort db_add_rec   = 0;
  24.  
  25. /*
  26.  *      db_create  -  Create a New Data File 
  27.  */
  28.  
  29. DATA_FILE db_create(path, fname, options)
  30.  char *path, *fname, *options;               
  31. {
  32.         BUFFER db_alloc_buf();
  33.         DATA_FILE df;
  34.     FILE_HDR  fhdr;
  35.         char  *fname_dflts(), *calloc();
  36.  
  37.         db_error = 0;
  38.  
  39.         df = (DATA_FILE) calloc(1, sizeof(struct db_data_file));
  40.     if (!df)
  41.     {    db_error = DB_ALLOC_ERROR;
  42.         return(NULL);
  43.     }
  44.  
  45.         df->df_fhdr = db_alloc_buf(DB_FILE_HDR_SIZE);
  46.         fhdr        = (FILE_HDR) df->df_fhdr->buf_data;
  47.     if (db_error) { db_free_df(df); return(NULL); }
  48.  
  49.         df->df_fhdr->buf_cur_size = DB_FILE_HDR_SIZE;
  50.  
  51.         strcpy(df->df_fname, fname_dflts(fname,path));
  52.  
  53.     db_file_options(fhdr, options);
  54.         if (db_error) { db_free_df(df); return(NULL); }
  55.  
  56.         db_fhdr_create(fhdr);
  57.         if (db_error) { db_free_df(df); return(NULL); }
  58.  
  59. #ifdef MSC
  60.     df->df_fd = open(df->df_fname, O_CREAT|O_TRUNC|O_RDWR|O_BINARY, S_IREAD|S_IWRITE);
  61. #endif
  62. #ifdef TURBO 
  63.     df->df_fd = open(df->df_fname, O_CREAT|O_TRUNC|O_RDWR|O_BINARY, S_IREAD|S_IWRITE);
  64. #endif
  65. #ifdef ULTRIX
  66.     df->df_fd = open(df->df_fname, O_CREAT|O_TRUNC|O_RDWR, S_IREAD|S_IWRITE);
  67. #endif
  68. #ifdef SYSV
  69.     df->df_fd = open(df->df_fname, O_CREAT|O_TRUNC|O_RDWR, S_IREAD|S_IWRITE);
  70. #endif
  71.         if (df->df_fd < 0)
  72.         {       db_error = DB_FILE_NOT_CREATED;
  73.                 db_free_df(df);
  74.                 return(NULL);
  75.         }                       
  76.  
  77.         df->df_stat = DB_OPEN;
  78.  
  79.         switch (fhdr->fh_file_type)
  80.         { 
  81. #ifdef IDX
  82.      case DB_INDEX:
  83.              df->df_buf = db_alloc_buf(fhdr->fh_block_size+2*fhdr->fh_rec_size);
  84.          if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
  85.  
  86.              df->df_tmp = db_alloc_buf(fhdr->fh_block_size+2*fhdr->fh_rec_size);
  87.          if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
  88.  
  89.              df->df_aux = db_alloc_buf(fhdr->fh_block_size+2*fhdr->fh_rec_size);
  90.          if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
  91.  
  92.              df->df_buf->buf_size = fhdr->fh_block_size;
  93.              df->df_tmp->buf_size = fhdr->fh_block_size;
  94.              df->df_aux->buf_size = fhdr->fh_block_size;
  95.          break;
  96. #endif
  97.  
  98. #ifdef RAN
  99.       case DB_RANDOM:
  100.              df->df_buf = db_alloc_buf(fhdr->fh_block_size);
  101.          if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
  102.  
  103.              df->df_tmp = db_alloc_buf(fhdr->fh_block_size);
  104.          if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
  105.  
  106.              while (fhdr->fh_last_block < fhdr->fh_base_size)
  107.              {  db_extend(df, df->df_buf);
  108.             if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
  109.  
  110.                 db_put_blk(df, df->df_buf);
  111.              }
  112.          break;
  113. #endif
  114.          
  115. #ifdef SEQ
  116.       case DB_SEQ:
  117.              df->df_buf = db_alloc_buf(fhdr->fh_block_size);
  118.          if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
  119.          break;
  120. #endif
  121.  
  122. #ifdef VAR
  123.       case DB_VAR:
  124.              df->df_buf = db_alloc_buf(fhdr->fh_block_size);
  125.          if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
  126.          break;
  127. #endif
  128.     }
  129.  
  130.         db_put_blk(df, df->df_fhdr);
  131.          if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
  132.  
  133.         return(df);
  134. }
  135.  
  136. /*
  137.  *    db_file_options - parse and setup file options
  138.  */
  139.  
  140. #define OPTION_SIZE     20
  141. #define SUB_OPTION_SIZE 50
  142.  
  143. db_file_options(fh, option_str)
  144.  FILE_HDR  fh;
  145.  char     *option_str;
  146. {
  147.     char  option[OPTION_SIZE], sub_option[SUB_OPTION_SIZE];
  148.     char *c, *o_ptr, *s_ptr;
  149.     int   cnt;
  150.  
  151.     c = option_str;
  152.     while (*c)
  153.     {    while (*c && !isalpha(*c)) c++;
  154.         
  155.         o_ptr  = option;
  156.         s_ptr  = sub_option;
  157.         *o_ptr = *s_ptr = NULL;
  158.  
  159.         for (cnt=0; isalpha(*c); cnt++, c++)
  160.         {    if (cnt < OPTION_SIZE)
  161.             {    if (islower(*c))
  162.                     *o_ptr++ = toupper(*c);
  163.                 else
  164.                     *o_ptr++ = *c;
  165.             }
  166.         }
  167.  
  168.         while (*c && *c != '=' && *c != ',') c++;
  169.  
  170.         if (*c == '=')
  171.         {    c++;
  172.             while (*c && *c <= ' ') c++;
  173.  
  174.             for (cnt=0; *c && *c!=',' && !isspace(*c); cnt++, c++)
  175.             {    if (cnt < SUB_OPTION_SIZE)
  176.                     *s_ptr++ = *c;
  177.             }
  178.  
  179.             while (*c && *c != ',') c++;
  180.         }
  181.  
  182.         *o_ptr++ = NULL;
  183.         *s_ptr++ = NULL;
  184.  
  185.         db_file_1option(fh, option, sub_option);
  186.         if (db_error) return;
  187.     }
  188. }
  189.  
  190. /*
  191.  *    db_file_1option - Process one file option
  192.  */
  193.  
  194. db_file_1option(fh, option, sub_option)
  195.  FILE_HDR  fh;
  196.  char     *option, *sub_option;
  197. {
  198.     if (strcmp(option, "SEQ")==0)
  199.     {    if (fh->fh_file_type == 0)
  200.             fh->fh_file_type = DB_SEQ;
  201.         else
  202.             db_error = DB_FTYPE_ERROR;
  203.     }
  204.     else
  205.     if (strcmp(option, "RAN")==0)
  206.     {    if (fh->fh_file_type == 0)
  207.             fh->fh_file_type = DB_RANDOM;
  208.         else
  209.             db_error = DB_FTYPE_ERROR;
  210.     }
  211.     else
  212.     if (strcmp(option, "IDX")==0)
  213.     {    if (fh->fh_file_type == 0)
  214.             fh->fh_file_type = DB_INDEX;
  215.         else
  216.             db_error = DB_FTYPE_ERROR;
  217.     }
  218.     else
  219.     if (strcmp(option, "VAR")==0)
  220.     {    if (fh->fh_file_type == 0)
  221.             fh->fh_file_type = DB_VAR;
  222.         else
  223.             db_error = DB_FTYPE_ERROR;
  224.     }
  225.     else
  226.     if (strcmp(option, "BLK")==0)
  227.         fh->fh_block_size = atoi(sub_option);
  228.     else
  229.     if (strcmp(option, "REC")==0)
  230.         fh->fh_data_size = atoi(sub_option);
  231.     else
  232.     if (strcmp(option, "BASE")==0)
  233.         fh->fh_base_size = atoi(sub_option);
  234.     else
  235.     if (strcmp(option, "KEY")==0)
  236.         fh->fh_key_size = atoi(sub_option);
  237.     else
  238.     if (strcmp(option, "ATR")==0)
  239.         fh->fh_atr_size = atoi(sub_option);
  240.     else
  241.     if (strcmp(option, "MLINKS")==0)
  242.         fh->fh_mlink_cnt = atoi(sub_option);
  243.     else
  244.     if (strcmp(option, "OLINKS")==0)
  245.         fh->fh_olink_cnt = atoi(sub_option);
  246.     else
  247.     if (strcmp(option, "KLINKS")==0)
  248.         fh->fh_klink_cnt = atoi(sub_option);
  249.     else
  250.     if (strcmp(option, "DUPS")==0)
  251.         fh->fh_file_stat &= ~DB_DUP_ALLOWED;
  252.     else
  253.     if (strcmp(option, "NODUPS")==0)
  254.         fh->fh_file_stat |= DB_DUP_ALLOWED;
  255.     else
  256.         db_error = DB_INVALID_OPTION;
  257. }
  258.  
  259. /*
  260.  *      db_fhdr_create - Check file header data on create
  261.  */
  262.  
  263. void db_fhdr_create(fh)
  264.  FILE_HDR fh;
  265. {       short hdr_size;
  266.  
  267.         db_error = 0;
  268.  
  269.         fh->fh_db_version = DB_VERSION;
  270.  
  271.         if (fh->fh_file_type != DB_SEQ    &&
  272.             fh->fh_file_type != DB_RANDOM &&
  273.             fh->fh_file_type != DB_INDEX  &&
  274.             fh->fh_file_type != DB_VAR)
  275.         {       db_error = DB_INVALID_FHDR;
  276.                 return;
  277.         }
  278.  
  279. #ifndef SEQ
  280.         if (fh->fh_file_type == DB_SEQ)
  281.         {       db_error = DB_UNSUPP_FEATURE;
  282.                 return;
  283.         }
  284. #endif
  285.  
  286. #ifndef RAN
  287.         if (fh->fh_file_type == DB_RANDOM)
  288.         {       db_error = DB_UNSUPP_FEATURE;
  289.                 return;
  290.         }
  291. #endif
  292.  
  293. #ifndef IDX
  294.         if (fh->fh_file_type == DB_INDEX)
  295.         {       db_error = DB_UNSUPP_FEATURE;
  296.                 return;
  297.         }
  298. #endif
  299.  
  300. #ifndef VAR
  301.         if (fh->fh_file_type == DB_VAR)
  302.         {       db_error = DB_UNSUP